home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_u_z / xlock_dv.zip / SOURCES.ZIP / QIX.C < prev    next >
C/C++ Source or Header  |  1992-08-28  |  4KB  |  208 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)qix.c 22.3 89/09/20";
  3. #endif
  4. /*-
  5.  * qix.c - The old standby vector swirl for the xlock X11 terminal locker.
  6.  *
  7.  * Copyright (c) 1989 by Sun Microsystems Inc.
  8.  *
  9.  * Permission to use, copy, modify, and distribute this software and its
  10.  * documentation for any purpose and without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and that
  12.  * both that copyright notice and this permission notice appear in
  13.  * supporting documentation.
  14.  *
  15.  * This file is provided AS IS with no warranties of any kind.  The author
  16.  * shall have no liability with respect to the infringement of copyrights,
  17.  * trade secrets or any patents by this file or any part thereof.  In no
  18.  * event will the author be liable for any lost revenue or profits or
  19.  * other special, indirect and consequential damages.
  20.  *
  21.  * Comments and additions should be sent to the author:
  22.  *
  23.  *               naughton@sun.com
  24.  *
  25.  *               Patrick J. Naughton
  26.  *               Window Systems Group, MS 14-40
  27.  *               Sun Microsystems, Inc.
  28.  *               2550 Garcia Ave
  29.  *               Mountain View, CA  94043
  30.  *
  31.  * Revision History:
  32.  * 20-Sep-89: Lint.
  33.  * 24-Mar-89: Written.
  34.  */
  35.  
  36. #include <X11/Xos.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/Xutil.h>
  39.  
  40. static Display *Dsp;
  41. static Window Win;
  42. static GC   Gc,
  43.         eraseGC = (GC) 0;
  44. static int  timeout;
  45. static int  Nlines;
  46. static int  color;
  47. static unsigned long pix = 0;
  48. static long startTime;
  49. static int  first,
  50.         last,
  51.         dx1,
  52.         dy1,
  53.         dx2,
  54.         dy2,
  55.         x1,
  56.         y1,
  57.         x2,
  58.         y2,
  59.         offset,
  60.         delta,
  61.         width,
  62.         height;
  63.  
  64. typedef struct {
  65.     int        x,
  66.         y;
  67. }        point;
  68.  
  69. static point *lineq = (point *) 0;
  70.  
  71. static long
  72. seconds()
  73. {
  74.     struct timeval foo;
  75.  
  76.     gettimeofday(&foo, (struct timezone *) 0);
  77.     return (foo.tv_sec);
  78. }
  79.  
  80. void
  81. initqix(d, w, g, c, t, n)
  82.     Display    *d;
  83.     Window    w;
  84.     GC        g;
  85.     int        c,
  86.         t,
  87.         n;
  88. {
  89.     XWindowAttributes xgwa;
  90.     XGCValues    xgcv;
  91.  
  92.     startTime = seconds();
  93.  
  94.     if ((lineq) || (n != Nlines)) {
  95.     if (lineq)
  96.         free((char *) lineq);
  97.     lineq = (point *) malloc(n * sizeof(point));
  98.     Nlines = n;
  99.     }
  100.     Dsp = d;
  101.     Win = w;
  102.     Gc = g;
  103.     color = c;
  104.     timeout = t;
  105.  
  106.     if (eraseGC == (GC) 0) {
  107.     xgcv.foreground = BlackPixel(Dsp, 0);
  108.     eraseGC = XCreateGC(Dsp, Win, GCForeground, &xgcv);
  109.     }
  110.     if (!color)
  111.     XSetForeground(Dsp, Gc, WhitePixel(Dsp, 0));
  112.  
  113.     XGetWindowAttributes(Dsp, Win, &xgwa);
  114.     width = xgwa.width;
  115.     height = xgwa.height;
  116.  
  117.     if (width < 100)        /* icon window */
  118.     delta = 2;
  119.     else
  120.     delta = 15;
  121.     offset = delta / 3;
  122.     last = 0;
  123.  
  124.     srandom(time((long *) 0));
  125.     dx1 = ((rand() >> 5) & (width - 1)) + 50;
  126.     dy1 = ((rand() >> 5) & (height - 1)) + 50;
  127.     dx2 = ((rand() >> 5) & (width - 1)) + 50;
  128.     dy2 = ((rand() >> 5) & (height - 1)) + 50;
  129.     x1 = (rand() >> 5) & width;
  130.     y1 = (rand() >> 5) & height;
  131.     x2 = (rand() >> 5) & width;
  132.     y2 = (rand() >> 5) & height;
  133.     XFillRectangle(Dsp, Win, eraseGC, 0, 0, width, height);
  134. }
  135.  
  136. int
  137. qixdone()
  138. {
  139.     return (seconds() - startTime > timeout);
  140. }
  141.  
  142. void
  143. drawqix()
  144. {
  145.     register int n = Nlines;
  146.  
  147.     while (n--) {
  148.     first = (last + 2) % Nlines;
  149.  
  150.     XDrawLine(Dsp, Win, eraseGC,
  151.           lineq[first].x, lineq[first].y,
  152.           lineq[first + 1].x, lineq[first + 1].y);
  153.     x1 += dx1;
  154.     y1 += dy1;
  155.     x2 += dx2;
  156.     y2 += dy2;
  157.     check_bounds_x(x1, &dx1);
  158.     check_bounds_y(y1, &dy1);
  159.     check_bounds_x(x2, &dx2);
  160.     check_bounds_y(y2, &dy2);
  161.     if (color) {
  162.         XSetForeground(Dsp, Gc, pix++);
  163.         pix %= 254;
  164.     }
  165.     XDrawLine(Dsp, Win, Gc, x1, y1, x2, y2);
  166.     en_queue(x1, y1, &last);
  167.     en_queue(x2, y2, &last);
  168.     }
  169. }
  170.  
  171. static
  172. en_queue(x, y, head)
  173.     int        x,
  174.         y;
  175.     int           *head;
  176. {
  177.  
  178.     lineq[*head].x = x;
  179.     lineq[*head].y = y;
  180.     *head = (*head + 1) % Nlines;
  181. }
  182.  
  183. static
  184. check_bounds_y(y, dy)
  185.     int        y,
  186.            *dy;
  187. {
  188.     if (y < 0) {
  189.     *dy = ((rand() >> 3) & delta) + offset;
  190.     }
  191.     if (y > height) {
  192.     *dy = -((rand() >> 3) & delta) - offset;
  193.     }
  194. }
  195.  
  196. static
  197. check_bounds_x(x, dx)
  198.     int        x,
  199.            *dx;
  200. {
  201.     if (x < 0) {
  202.     *dx = ((rand() >> 3) & delta) + offset;
  203.     }
  204.     if (x > width) {
  205.     *dx = -((rand() >> 3) & delta) - offset;
  206.     }
  207. }
  208.